define([
    "require",
    "jQuery",
    "core",
    "select2"
], function (require, jQuery, core, select2) {
    var select = {};

    function scanSelect(options) {
        $(".select2").select2(options);
    }

    function scanDictSelect(dictUrl, options) {
        if (!dictUrl) dictUrl = "/base/dict/rest/findByGroup";
        if (!options) options = {};
        var elemList = $("select[data-dict]");
        elemList.each(function (e) {
            var select = $(this);
            var group = select.data("dict");
            var value = select.data("value");
            $.get(core.getUrl(dictUrl) + "/" + group.toUpperCase(), function (data) {
                data.forEach(function (record) {
                    if (value === record.code || value.toString() === record.code)
                        select.append('<option value="' + record.code + '" selected="selected">' + record.name + '</option>');
                    else
                        select.append('<option value="' + record.code + '">' + record.name + '</option>');
                })
                select.select2(options);
            })
        });
    }

    function scanUrlSelect(options) {
        if (!options) options = {};

        $("select[data-url]").each(function (e) {
            var select = $(this);
            if (select.data("init") != null) return;
            var url = select.data("url");
            var value = select.data("value");
            var key = select.data("key");
            var name = select.data("name");
            var selectType = select.data("type");
            var groupName = select.data("groupName");
            if (!key) key = "code";
            if (!name) name = "name";
            if (!selectType) selectType = "normal";
            if (!groupName) groupName = "name";
            if (url != undefined) {
                $.ajax({
                    url: core.getUrl(url),
                    success: function (data) {
                        switch (selectType) {
                            case "group":
                                select.append("<option></option>");
                                data.forEach(function (record) {
                                    record["options"].forEach(function (r) {
                                        if (r[key] != "" && value.toString() == r[key]) {
                                            html += '<option value="' + r[key] + '" selected="selected" ' +
                                                'data-type="' + record.id + '">' + r[name] + '</option>';
                                        }
                                        else {
                                            html += '<option value="' + r[key] + '" ' +
                                                'data-type="' + record.id + '">' + r[name] + '</option>';
                                        }
                                    })
                                    var groupLabel = record[groupName];

                                    var html = '<optgroup label="' + groupLabel + '">';

                                    html += '</optgroup>';
                                    select.append(html);
                                });
                                break;
                            default:
                                select.append("<option></option>");
                                data.forEach(function (record) {
                                    var values = value.toString().split(",");
                                    var selected = false;
                                    for (var i = 0; i < values.length; i++) {
                                        if (record[key] != "" && values[i] == (record[key])) selected = true;
                                    }
                                    if (selected) {
                                        select.append('<option value="' + record[key] + '" selected="selected">'
                                            + record[name] + '</option>');
                                    }
                                    else {
                                        select.append('<option value="' + record[key] + '">' + record[name] + '</option>');
                                    }
                                });
                                break;
                        }

                        select.select2(options);
                        select.data("init", true);
                    }
                })
            }
        });
    }

    select.initSelect = function (dictUrl, options) {
        if (typeof dictUrl != 'string') {
            options = dictUrl;
            dictUrl = undefined;
        }

        options = $.extend({}, {
            minimumResultsForSearch: Infinity
        }, options);

        scanDictSelect(dictUrl, options);
        scanUrlSelect(options);
        scanSelect(options);
    };

    return select;
})